package com.kotlin.zds.activity

import android.view.LayoutInflater
import com.alibaba.android.arouter.facade.annotation.Route
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import com.kotlin.zds.arouter.Routes
import com.kotlin.zds.base.BaseActivity
import com.kotlin.zds.databinding.ActivityGdlocationBinding
import com.xuexiang.xui.utils.StatusBarUtils


/**
 * Author: zds
 * Date: 2022/11/7 11:54
 * Description: https://lbs.amap.com/api/android-location-sdk/guide/android-location/getlocation
 * Version: v1.0
 */
@Route(path = Routes.PATH_GAODE2)
class GdLocationActivity:BaseActivity(), AMapLocationListener {
    val viewBinding by lazy {
        ActivityGdlocationBinding.inflate(LayoutInflater.from(mActivity))
    }

    //声明AMapLocationClient类对象
    var mLocationClient: AMapLocationClient? = null
    //声明AMapLocationClientOption对象
    var mLocationOption: AMapLocationClientOption? = null

    override fun onSetContentView() {
        StatusBarUtils.initStatusBarStyle(this,false)
       setContentView(viewBinding.root)
    }

    override fun initView() {
        viewBinding.run {
            btnStar.setOnClickListener {
                //启动定位 需要判断是否拥有定位权限，这里偷懒就不写了
                mLocationClient?.startLocation();
            }
        }

    }

    override fun initData() {
        //初始化定位
        mLocationClient = AMapLocationClient(this)
        //设置定位回调监听
        mLocationClient?.setLocationListener(this)
        //初始化AMapLocationClientOption对象-用户配置参数的对象
        mLocationOption = AMapLocationClientOption()
        //（签到、出行、运动，默认无场景）
        mLocationOption?.locationPurpose = AMapLocationClientOption.AMapLocationPurpose.SignIn
        //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        mLocationOption?.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy;
        //获取最近3s内精度最高的一次定位结果：
        mLocationOption?.isOnceLocationLatest = true;
        //设置定位间隔,单位毫秒,默认为2000ms，最低1000ms。
        mLocationOption?.interval = 1000;
        //给定位客户端对象设置定位参数
        mLocationClient?.setLocationOption(mLocationOption)

    }

    override fun onLocationChanged(p0: AMapLocation?) {
        viewBinding.run {
            tvLocation.text = p0?.address
            tvLocation2.text = p0?.street
            tvLocation3.text = p0?.aoiName
            tvLocation4.text = "${p0?.longitude},${p0?.latitude}"
            mLocationClient?.stopLocation()
        }

    }
}